package com.xnck.example.jertest.aspect;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 测试用AOP拦截，被事务包裹
 */
@Aspect
@Order(3)
@Component
public class TestAfterTranAspect {

    /**
     * 定义Pointcut，Pointcut的名称，此方法不能有返回值，该方法只是一个标示
     */
    @Pointcut("@annotation(com.xnck.example.jertest.annotation.AspectAfterTran)")
    public void aspectAfterTran()
    {
        System.out.println("this is AspectAfterTran");
    }

    /**
     * 定义Pointcut，Pointcut的名称，此方法不能有返回值，该方法只是一个标示
     */
    @Pointcut("@annotation(com.xnck.example.jertest.annotation.AspectRoundTran)")
    public void aspectRount()
    {
        System.out.println("this is aspectRount");
    }

    @After("aspectAfterTran()")
    public void afterRun(JoinPoint joinPoint) throws Exception {
        throw new Exception("给你个中断异常");
    }

    @Around("aspectRount()")
    public Object roundRun(ProceedingJoinPoint joinPoint) {
        Object result = null;
        try {
            System.out.println(Arrays.asList(joinPoint.getArgs()));
            result = joinPoint.proceed();
            if (null == result) {
                throw new Exception("给你个中断异常");
            }
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
